/*
 * MemAudio.cpp
 *
 *  Created on: 15 Sep, 2017
 *      Author: buffon
 */

#include "MemAudio.h"
#include <stdio.h>
MemAudio::MemAudio(){
		pCurBuffer = pNextBuffer = pNewBuffer = pSaveBuffer = NULL;
		pSaveBuffer = new char[MAX_SUPPORT_SIZE];
		memset(pSaveBuffer,0,MAX_SUPPORT_SIZE);

		pNewBuffer = new char[MAX_SUPPORT_SIZE];
		memset(pNewBuffer,0,MAX_SUPPORT_SIZE);

		pCurBuffer = pSaveBuffer;
		pNextBuffer = pNewBuffer;
		nIndex = curLen =lenBytes = lenBytesCp =0;

		bSaveIsMaster = true;
		for(int i = 0; i < TIME_INTERVAL; i++)
			nLenArray[i] = 0;

		worker = new CWorkerThread();
		if (!worker) {
			cout << "create worker failed!"<<endl;
		}
		worker->Start();

	}
MemAudio::~MemAudio(){
		if(pSaveBuffer){
			delete pSaveBuffer;
			pSaveBuffer = NULL;
		}
		if(pNewBuffer){
			delete pNewBuffer;
			pNewBuffer = NULL;
		}
		pCurBuffer = NULL;

		if(worker){
			delete worker;
			worker = NULL;
		}
	}

//bool MemAudio::saveToFile(char* pData,int length){
//	bool bOk = false;
//	if(length > 132000){
//		FILE *file = fopen("tmp.m4a","wb");
//		if(file){
//			fwrite(pData,length,1,file);
//			fclose(file);
//		}
//		bOk = true;
//	}
//	return bOk;
//}

int MemAudio::appendBuffer(char* pData,int length,bool flag){

	struct timeval      curTime;
	gettimeofday(&curTime,NULL);

	if(lenBytes == 0){  // the first writing
		prevTime.tv_sec = curTime.tv_sec;
		prevTime.tv_usec = curTime.tv_usec;
		nLenArray[nIndex] = lenBytes;
		nIndex ++;
	}

	memcpy(pCurBuffer+ lenBytes,pData,length);
	lenBytes += length;

	double interval = curTime.tv_sec*1000 + curTime.tv_usec/1000 - (prevTime.tv_sec*1000 + prevTime.tv_usec/1000);

	cout << "time interval:"<<lenBytes<<' '<<nIndex<<' '<<curTime.tv_sec*1000 + curTime.tv_usec/1000<<' '<<prevTime.tv_sec*1000 + prevTime.tv_usec/1000<<' '<<interval<<' '<<endl;
	if(flag&& interval/ 1000 > TIME_INTERVAL){//CLOCKS_PER_SEC
			prevTime = curTime;
			if(bReady){
				// addTask
				TimerTask* pTask = new TimerTask(pCurBuffer,nLenArray[nIndex],lenBytes +length,2,"http://mmkyf.maimaicn.com/mmjmanager/yaotv/tvUpload.action");
				worker->PushTask(pTask);
			}
			if(!bReady && (nIndex+1) == ARRAY_LEN){
				bReady = true;
			}
			nLenArray[nIndex] = lenBytes +length;
			nIndex = (nIndex +1)% ARRAY_LEN;
			curLen = lenBytes + length;
			cout << "TIME_INTERVAL"<<nIndex<<' '<<bReady<<endl;
	}

	if(lenBytes > MAX_STD_SIZE){
		memcpy(pNextBuffer+ lenBytesCp,pData,length);
		lenBytesCp += length;
	}

	/*clock_t end = clock();
	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
	cout <<"memcpy elapsed secs:"<< elapsed_secs <<endl;*/

	if( lenBytes > SWITCH_SIZE ){

		cout<< "switch begin..."<<endl;

		pCurBuffer = bSaveIsMaster?pNewBuffer:pSaveBuffer;
		lenBytes = lenBytesCp;
		cout<< "switch end..."<<endl;
		pNextBuffer = bSaveIsMaster?pSaveBuffer:pNewBuffer;
		bSaveIsMaster = !bSaveIsMaster;
		lenBytesCp = 0;

		time_t ts = time(NULL);
		tm* pT = localtime(&ts);
		char audioFile[100] = {0};
//			sprintf(audioFile,"audio_%02d_%02d_%02d.pcm",pT->tm_hour,pT->tm_min,pT->tm_sec);
	}
	return length;
}
